package cn.mw.cmdb.param;

import cn.mw.cmdb.entity.MwComponentLayoutInfo;
import cn.mw.cmdb.service.ServiceChoose;
import cn.mw.cmdb.service.impl.InstanceMonitorServiceImpl;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import com.google.common.base.Strings;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

@Data
public class ComponentLayoutSearchParam extends SearchParam implements ServiceChoose, SearchParamConvert {
    //实例id
    private String instanceId;
    //模板name
    private String templateName;
    //模板name
    private List<String> templateNames;
    //是否使用实例Id查询(使用：自定义查询，不使用：通用查询)
    private boolean isCommonFlag;

    @Override
    public String getServiceId() {
        return InstanceMonitorServiceImpl.ID;
    }

    @Override
    public void convert() {
        if (CollectionUtils.isNotEmpty(getTemplateNames())) {
            this.addSubCondition(CMDBTool.getFieldName(MwComponentLayoutInfo::getTempName)
                    , new SubCondition(CriteriaOpsType.in.name(), getTemplateNames()));
        }
        if (!Strings.isNullOrEmpty(getTemplateName())) {
            this.addSubCondition(CMDBTool.getFieldName(MwComponentLayoutInfo::getTempName)
                    , new SubCondition(CriteriaOpsType.is.name(), getTemplateName()));
        }
        //是否自定义查询
        if (isCommonFlag) {
            this.addSubCondition(CMDBTool.getFieldName(MwComponentLayoutInfo::getAssetsId)
                    , new SubCondition(CriteriaOpsType.exist.name(), false));
        } else {
            if (!Strings.isNullOrEmpty(getInstanceId())) {
                this.addSubCondition(CMDBTool.getFieldName(MwComponentLayoutInfo::getAssetsId)
                        , new SubCondition(CriteriaOpsType.is.name(), getInstanceId()));
            }
        }
    }

}
